<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2019-02-28
 * Time: 23:54
 */

class udp
{
    const HOST = '0.0.0.0';
    const PORT = 9501;
    public $serv = null;

    public function __construct()
    {
        $this->serv = new swoole_server(self::HOST, self::PORT, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
        $this->serv->set([
            'worker_num' => 8, //worker进程数 CPU核数的1-4倍
            'max_request' => 2000 //表示worker进程在处理完n次请求后结束运行
        ]);
        $this->serv->on('Packet', [$this, 'onPacket']);
        $this->serv->start();
    }

    public function onPacket($serv, $data, $clientInfo)
    {
        $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
        var_dump($clientInfo);
    }
}

$serv = new udp();